﻿using Abp.BackgroundJobs;
using Abp.Dependency;
using Abp.Domain.Repositories;
using Abp.Domain.Uow;
using Mt.Site.Core.ContentManagement.Entities;
using Mt.Site.Core.Synchronization;
using Mt.Site.Core.Synchronization.ContentManagement;
using Mt.Site.Core.Synchronization.Data;
using System;

namespace Mt.Site.Core.BackGroundJobs.ContentManagement
{
    public class SynchorizationContentCustomRouterJob : BackgroundJob<BaseSyncArgs>, ITransientDependency
    {
        private readonly IContentAutoSyncManager _contentAutoSyncManager;

        private readonly IBackgroundJobManager _backgroundJobManager;

        private readonly ISynchronizationHelper _synchronizationHelper;

        private readonly IRepository<Content, Guid> _contentRepository;

        public SynchorizationContentCustomRouterJob(
                IContentAutoSyncManager contentAutoSyncManager,
                IBackgroundJobManager backgroundJobManager,
                ISynchronizationHelper synchronizationHelper,
                IRepository<Content, Guid> contentRepository)
        {
            _contentAutoSyncManager = contentAutoSyncManager;
            _backgroundJobManager = backgroundJobManager;
            _synchronizationHelper = synchronizationHelper;
            _contentRepository = contentRepository;
        }

        [UnitOfWork]
        public override void Execute(BaseSyncArgs args)
        {
            using (CurrentUnitOfWork.DisableFilter(
                AbpDataFilters.MayHaveTenant,
                AbpDataFilters.MustHaveTenant,
                MtSiteConsts.AbpDataFilters.MustHaveLanguage))
            {
                var entity = _contentRepository.FirstOrDefault(args.Id);

                if (entity == null)
                {
                    return;
                }

                var sonSites = _synchronizationHelper.GetSonSite(args.FromTenantId, args.FromLanguage, entity.MenuId);

                foreach (var item in sonSites)
                {
                    var toId = Guid.Empty;
                    if (!item.AutoSynchronous)
                    {
                        continue;
                    }
                    var createOrUpdateSyncStore = new CustomRouterSyncDto
                    {
                        FromContentId = args.Id,
                        ToLanguage = item.TargetLanguage,
                        ToTenantId = item.TargetTenantId
                    };

                    toId = _contentAutoSyncManager.CreateOrUpdateContentCustomRouter(createOrUpdateSyncStore);

                    if (toId != Guid.Empty)
                    {
                        var nextSites = _synchronizationHelper.GetSonSite(item.TargetTenantId, item.TargetLanguage, entity.MenuId);
                        if (nextSites.Count > 0)
                        {
                            _backgroundJobManager.Enqueue<SynchorizationContentCustomRouterJob, BaseSyncArgs>(
                                                        new BaseSyncArgs
                                                        {
                                                            Id = toId,
                                                            FromLanguage = item.TargetLanguage,
                                                            FromTenantId = item.TargetTenantId,
                                                        });
                        }

                    }
                }
            }
        }
    }
}
